//
//  TTLuoshiCellView.m
//  TaoTuo
//
//  Created by apple on 2020/11/25.
//  Copyright © 2020 刘广. All rights reserved.
//

#import "TTLuoshiCellView.h"
#import "TTCommondDataModel.h"

@interface TTLuoshiCellView ()

@property (nonatomic, strong) UIImageView *luoshiImageView;
@property (nonatomic, strong) UILabel *luoshiLabel;
/// 标题
@property (nonatomic, strong) UILabel *titleLabel;
/// 奶咖
@property (nonatomic, strong) UILabel *nkLabel;
/// 形状
@property (nonatomic, strong) UILabel *shapeLabel;
/// 证书号
@property (nonatomic, strong) UILabel *zsNumLabel;
/// 证书预览按钮
@property (nonatomic, strong) UIButton *zsBtn;
/// 价格
@property (nonatomic, strong) UILabel *priceLabel;
/// 售罄或下架图片
@property (nonatomic, strong) UIImageView *selloutImageView;

/// 证书颁发日期
@property (nonatomic, strong) UILabel *issueDateLabel;

@end

@implementation TTLuoshiCellView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
//        [self setupSubviews];
        
    }
    return self;
}

- (void)lookZSBtnClick
{
//    if ([self.delegate respondsToSelector:@selector(clickLookCertificateWithModel:)]) {
//        [self.delegate clickLookCertificateWithModel:self.model];
//    }
}

- (void)setDetailModel:(TTCustomChooseLuoshiModel *)detailModel
{
    _detailModel = detailModel;
    
    NSString *urlString = [detailModel.GoodsImage stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    NSURL *imageUrl = [NSURL URLWithString:urlString];
    
    [self.luoshiImageView sd_setImageWithURL:imageUrl placeholderImage:[UIImage imageNamed:@"yuanluoshi"] options:SDWebImageRetryFailed];
    
    self.luoshiLabel.text = [TTCommondDataModel toShenZhenTime][detailModel.GoodsLocation];
    self.titleLabel.text = detailModel.GoodsTitle;
    if (detailModel.GoodsDescribeTemp.count == 1) {
        NSString *string = detailModel.GoodsDescribeTemp.firstObject;
        string = [string stringByReplacingOccurrencesOfString:@"\"" withString:@""];
        string = [string stringByReplacingOccurrencesOfString:@"]" withString:@""];
        string = [string stringByReplacingOccurrencesOfString:@"[" withString:@""];
        self.nkLabel.text = string;
    } else {
        self.nkLabel.text = [detailModel.GoodsDescribeTemp componentsJoinedByString:@" "];
    }
    
    NSString *value = @" ";
    if (detailModel.ClarityCharacteristics.length > 1) {
        NSDictionary *dic = [TTCommondDataModel getClarityCharacteristics];
        NSArray *characteristicsArray = [detailModel.ClarityCharacteristics componentsSeparatedByString:@","];
        for (NSString *str in characteristicsArray) {
            NSString *key = [str removeFirstAndLastBlank];
            value = [NSString stringWithFormat:@"%@%@ ", value, dic[key]];
        }
    }
    self.shapeLabel.text = value;
    
    self.zsBtn.hidden = detailModel.GoodsCertPic.length == 0;
    // 证书号
//    NSString *zsNum = detailModel.SouthAfrica ? @"South Africa" : [NSString stringWithFormat:@"%@ %@", detailModel.GoodsCertType, detailModel.GoodsCertNo];
    self.zsNumLabel.text = [NSString stringWithFormat:@"%@ %@", detailModel.GoodsCertType, detailModel.GoodsCertNo];
    
    self.priceLabel.text = [NSString stringWithFormat:@"￥%@", detailModel.GoodsPrice];
    
    self.issueDateLabel.text = @"";
    
    // 售罄或下架处理
    if (detailModel.GoodsState == 1) {
        self.selloutImageView.hidden = YES;
        self.titleLabel.textColor = [UIColor blackColor];
        self.nkLabel.textColor = UIColorHex(333333);
        self.priceLabel.textColor = UIColorHex(FF1F1F);
    } else {
        self.selloutImageView.hidden = NO;
        NSString *sellImageName = detailModel.GoodsState == 2 ? @"shopcartsellout" : @"xiajia";
        self.selloutImageView.image = [UIImage imageNamed:sellImageName];
        
        self.titleLabel.textColor = UIColorHex(999999);
        self.nkLabel.textColor = UIColorHex(999999);
        self.priceLabel.textColor = UIColorHex(FF8D8D);
    }
    
    
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.luoshiImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(10);
        make.left.mas_equalTo(10);
        make.width.height.mas_equalTo(65);
    }];
    
    [self.selloutImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.luoshiImageView);
    }];
    
    [self.luoshiLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.luoshiImageView.mas_bottom).mas_offset(5);
        make.centerX.equalTo(self.luoshiImageView);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.luoshiImageView);
        make.left.equalTo(self.luoshiImageView.mas_right).mas_offset(10);
        make.right.lessThanOrEqualTo(self).mas_offset(-10);
    }];
    
    [self.nkLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).mas_offset(3);
        make.left.equalTo(self.titleLabel);
    }];
    
    [self.shapeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.nkLabel.mas_bottom).mas_offset(2);
        make.left.equalTo(self.titleLabel);
        make.right.equalTo(self.mas_right).offset(-10);
    }];
    
    [self.zsNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.shapeLabel.mas_bottom);
        make.left.equalTo(self.titleLabel);
    }];
    
    [self.zsBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.zsNumLabel);
        make.left.equalTo(self.zsNumLabel.mas_right).offset(5);
    }];
    
    [self.priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).mas_offset(-10);
        make.centerY.equalTo(self.zsNumLabel);
    }];
    
    [self.issueDateLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.zsNumLabel.mas_bottom).offset(5);
        make.left.equalTo(self.zsNumLabel);
    }];
    
    [self layoutIfNeeded];
    
    CGFloat b = self.issueDateLabel.bottom + 10;
    self.height = b < 100 ? 100 : b;
    
}









- (UIImageView *)luoshiImageView
{
    if (!_luoshiImageView) {
        _luoshiImageView = [[UIImageView alloc] init];
        _luoshiImageView.contentMode = UIViewContentModeScaleAspectFill;
        _luoshiImageView.layer.cornerRadius = 10;
        _luoshiImageView.layer.masksToBounds = YES;
        [self addSubview:_luoshiImageView];
    }
    return _luoshiImageView;
}

- (UIImageView *)selloutImageView
{
    if (!_selloutImageView) {
        _selloutImageView = [[UIImageView alloc] init];
        _selloutImageView.contentMode = UIViewContentModeCenter;
        [self addSubview:_selloutImageView];
    }
    return _selloutImageView;
}

- (UILabel *)luoshiLabel
{
    if (!_luoshiLabel) {
        _luoshiLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:9];
        [self addSubview:_luoshiLabel];
    }
    return _luoshiLabel;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [UILabel labelWithTitle:@"" color:[UIColor blackColor] font:[UIFont systemFontOfSize:15]];
        [self addSubview:_titleLabel];
    }
    return _titleLabel;
}

- (UILabel *)nkLabel
{
    if (!_nkLabel) {
        _nkLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:12];
        [self addSubview:_nkLabel];
    }
    return _nkLabel;
}


- (UILabel *)shapeLabel
{
    if (!_shapeLabel) {
        _shapeLabel = [UILabel labelWithTitle:@"" color:UIColorHex(999999) fontSize:8];
        [self addSubview:_shapeLabel];
    }
    return _shapeLabel;
}

- (UILabel *)zsNumLabel
{
    if (!_zsNumLabel) {
        _zsNumLabel = [UILabel labelWithTitle:@"" color:UIColorHex(333333) fontSize:12];
        [self addSubview:_zsNumLabel];
    }
    return _zsNumLabel;
}


- (UIButton *)zsBtn
{
    if (!_zsBtn) {
        _zsBtn = [UIButton createButtonWithTitle:@"" titleColor:kMainColor titleFont:[UIFont systemFontOfSize:11] backgroundColor:nil target:self action:@selector(lookZSBtnClick)];
        
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:@"证书预览"];
        [attr addAttributes:@{NSUnderlineStyleAttributeName : @1, NSUnderlineColorAttributeName : kMainColor, NSForegroundColorAttributeName : kMainColor} range:[attr.string rangeOfAll]];
        [_zsBtn setAttributedTitle:attr forState:UIControlStateNormal];
        
        [self addSubview:_zsBtn];
    }
    return _zsBtn;
}


- (UILabel *)priceLabel
{
    if (!_priceLabel) {
        _priceLabel = [UILabel labelWithTitle:@"" color:UIColorHex(FF1F1F) fontSize:15];
        [self addSubview:_priceLabel];
    }
    return _priceLabel;
}


- (UILabel *)issueDateLabel
{
    if (!_issueDateLabel) {
        _issueDateLabel = [UILabel labelWithTitle:@"" color:UIColor.orangeColor fontSize:12];
        [self addSubview:_issueDateLabel];
    }
    return _issueDateLabel;
}


@end
